package com.hnucm.cooklife.presenter.searchwords;

import com.hnucm.cooklife.base.BasePresenter;
import com.hnucm.cooklife.bean.search.SearchData;
import com.hnucm.cooklife.contract.searchword.Contract;
import com.hnucm.cooklife.model.SearchWordModel;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class SearchWordPresenter extends BasePresenter<Contract.ISearchWordView> implements Contract.ISearchWordPresenter {

    Contract.ISearchWordModel mISearchWordModel;

    public SearchWordPresenter() {
        mISearchWordModel = new SearchWordModel();
    }

    @Override
    public void loadSearchWordData() {
        mISearchWordModel.loadSearchWordData()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<SearchData>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        mCompositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(@NonNull SearchData searchData) {
                        if(isViewAttached()){
                            getView().onloadSearhWord(searchData);
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
